﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace BS_2
{
    public partial class Form_NewSub : CCWin.Skin_DevExpress
    {
        public Form_NewSub()
        {
            InitializeComponent();
        }
        private void submit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("确认新建子区吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                int prjcode = Globale.project_code;
                string name = prjname.Text.Trim();
                string shuoming = prjshuoming.Text.Trim();

                string date = DateTime.Now.ToString("yyyy-MM-dd");

                int rows = Convert.ToInt16(dataGridView1.Rows.Count.ToString()) - 1;
                if (name == "" || shuoming == "")
                {
                    MessageBoxEx.Show("数据填写不完整，请检查！", "输入提示");
                    return;
                }

                if (rows == 0)
                {
                    MessageBoxEx.Show("请输入子区范围拐点坐标！", "输入提示");
                    return;
                }
                else if (rows < 3)
                {
                    MessageBoxEx.Show("至少输入3个拐点坐标！", "输入提示");
                    return;
                }
                else
                {
                    for (int i = 0; i < rows; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null || dataGridView1.Rows[i].Cells[1].Value == null)
                        {
                            MessageBoxEx.Show("拐点坐标必须填写完整！", "输入提示");
                            return;
                        }
                    }
                }
                string sql = "select subname from subgeo where subname='" + name + "' and prjcode=" + prjcode;
                string resultquery = DataAccess.Get_Row1_Col1_Value(sql);
                if (resultquery != "")
                {
                    MessageBoxEx.Show("子区名称已存在，请换名！", "输入提示");
                    return;
                }
                //录入子区信息
                sql = "insert into subgeo(prjcode,subname,subdescribe,subdate) values(" + prjcode + ",'" + name + "','" + shuoming + "','" + date + "')";
                int result = DataAccess.Run_SQL(sql);
                if (result == 1)
                {
                    //录入子区坐标
                    sql = "select subcode from subgeo where prjcode=" + prjcode + " and subname='" + name + "'";
                    string datacode = DataAccess.Get_Row1_Col1_Value(sql);
                    for (int i = 0; i < rows; i++)
                    {
                        int j = i + 1;
                        string sql_domain = "(" + datacode + "," + j + "," + dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[1].Value + ")";
                        sql = "insert into subgeo_domain values" + sql_domain;
                        int resultdomain = DataAccess.Run_SQL(sql);
                        if (resultdomain == 0)
                        {
                            sql = "delete from subgeo where prjcode=" + prjcode + " and subcode=" + datacode;
                            DataAccess.Run_SQL(sql);
                            sql = "delete from subgeo_domain where subcode=" + datacode;
                            DataAccess.Run_SQL(sql);
                            MessageBoxEx.Show("新建子区失败！", "提示");
                            return;
                        }
                    }

                    MessageBoxEx.Show("新建子区成功！", "提示");
                    ClearText();
                }
                else
                {
                    MessageBoxEx.Show("新建子区失败！", "提示");
                }
            }
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);

        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (Regex.IsMatch(e.FormattedValue.ToString(), @"^[+-]?\d*[.]?\d*$") == false)
            {
                e.Cancel = true;
                MessageBoxEx.Show("拐点坐标必须是数值型");
                return;
            }
        }

        private void FrmPrjNew_Load(object sender, EventArgs e)
        {

        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SkinLabel2_Click(object sender, EventArgs e)
        {

        }
        private void ClearText()
        {
            prjname.Text = "";
            prjshuoming.Text = "";
            dataGridView1.Rows.Clear();
        }
    }
}
